<?php


namespace app\singleton;


class InitSingleton
{
    private static $instance;
    private $paramA;
    private $paramB;

    private function __construct($paramA, $paramB)
    {
        $this->paramA = $paramA;
        $this->paramB = $paramB;
    }

    public static function getInstance()
    {
        if (self::$instance == null) {
            throw new \Exception('Run init() first.');
        }
        return self::$instance;
    }

    /**
     * 第二次传入的参数会无效，第一次传入init(10,50)，第二次传入init(20,30)，第二次的20,30没有生效
     * @param $paramA
     * @param $paramB
     * @return InitSingleton
     * @throws \Exception
     */
    public static function init($paramA, $paramB)
    {
        if (self::$instance != null) {
            throw new \Exception('Singleton has been created!');
        }
        self::$instance = new self($paramA, $paramB);
        return self::$instance;
    }
}